package com.example.newhabitclock

import android.app.AlarmManager
import android.content.Context.ALARM_SERVICE
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
import com.example.newhabitclock.databinding.FragmentAddHabitBinding

//添加习惯的界面（含习惯具体信息）
class AddHabitFragment : Fragment() {
    private lateinit var mBinding: FragmentAddHabitBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = FragmentAddHabitBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun onResume() {
        super.onResume()

        mBinding.saveButton.setOnClickListener {
            //先确保用户将习惯的信息输入完成
            val habitName = mBinding.habitNameInput.text.toString()
            val hour = mBinding.timePicker.hour
            val minute = mBinding.timePicker.minute
            //将时间以09：05显示而不是9：5
            val formattedTime = String.format("%02d:%02d", hour, minute)
            //收集checkBox中选择的日期
            val repeatDays = listOf(
                mBinding.checkMon,
                mBinding.checkTue,
                mBinding.checkWed,
                mBinding.checkThu,
                mBinding.checkFri,
                mBinding.checkSat,
                mBinding.checkSun,
            ).filter { it.isChecked }.map { it.text.toString() }.toTypedArray()  //转换为Array<String>

            val bundle = Bundle()

            /*mBinding.habitNameInput.text是一个Editable对象，调用toString()会将其转换为 String。
            即使用户没有输入任何内容，toString()也会返回一个空字符串 ""，而不会返回 null。
            即使没有任何复选框被勾选，repeatDays 也会是一个空数组（emptyArray()），而不会为 null。
            !repeatDays.contentEquals(emptyArray<String>())等同于repeatDays != emptyArray<String>()*/
            if (habitName != "" && !repeatDays.contentEquals(emptyArray<String>())) {
                bundle.putString("name", habitName)
                bundle.putString("time", formattedTime)
                bundle.putStringArray("repeatDays", repeatDays)
                setFragmentResult("requestKey", bundle)
                parentFragmentManager.popBackStack()  //将当前界面出栈，回退到MainFragment界面中
            } else if (habitName == "" && !repeatDays.contentEquals(emptyArray<String>())) {
                Toast.makeText(requireContext(), "请输入习惯名称", Toast.LENGTH_SHORT).show()
            } else if (habitName != "" && repeatDays.contentEquals(emptyArray<String>())) {
                Toast.makeText(requireContext(), "请输入习惯提醒日期", Toast.LENGTH_SHORT).show()
            } else if (habitName == "" && repeatDays.contentEquals(emptyArray<String>())) {
                Toast.makeText(requireContext(), "请输入习惯名称,提醒日期", Toast.LENGTH_SHORT)
                    .show()
            }
        }
    }

}